//
//  CZVistorView.swift
//  weibo
//
//  Created by Apple on 16/3/9.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit
import SnapKit

protocol CZVistorViewDelegate: NSObjectProtocol {
    func registerButtonSelected()
    func loginButtonSelected()
}

class CZVistorView: UIView {

    //  设置代理
    weak var delegate: CZVistorViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpVistor()
        //  开启旋转动画
        //startAnimation()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: 懒加载控件
    private lazy var iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var cycleView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        button.setTitle("注册", forState: .Normal)
        button.addTarget(self, action: "registerAction", forControlEvents: .TouchUpInside)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return button
        
    }()
    
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        button.setTitle("登录", forState: .Normal)
        button.addTarget(self, action: "loginAction", forControlEvents: .TouchUpInside)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return button
        
    }()
    
    private lazy var shadowView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    // MARK:    设置访客视图
    private func setUpVistor() {
        //  添加控件
        addSubview(iconView)
        addSubview(cycleView)
        addSubview(shadowView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
//        test()
        //  添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        cycleView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView.snp_center)
        }
        shadowView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView.snp_center)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(cycleView.snp_bottom)
            make.centerX.equalTo(cycleView.snp_centerX)
            make.width.equalTo(224)
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.leading.equalTo(messageLabel.snp_leading)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.trailing.equalTo(messageLabel.snp_trailing)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
        
    }
    
    // MARK:    执行逻辑
    private func startAnimation() {
    
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //  设置结束的弧度
        animation.toValue = 2 * M_PI
        //  设置动画执行时间
        animation.duration = 20
        //  设置执行次数
        animation.repeatCount = MAXFLOAT
        //  执行完成后,不移除动画
        animation.removedOnCompletion = false
        cycleView.layer.addAnimation(animation, forKey: "cycleViewAnimation")
        
    }
    
    //  停止动画
    private func stopAnimation() {
        //  移除动画
        cycleView.layer.removeAnimationForKey("cycleViewAnimation")

        
    }
    
    //  设置更新访客视图的图片和内容
    func setVisitorInfo(imgName: String?, message: String?) {
        
        if let img = imgName, msg = message {
            //  如果图片和消息文本不为nil说明是消息，发现，我页面
            iconView.image = UIImage(named: img)
            messageLabel.text = msg
            //  先停止动画
            stopAnimation()
            //  隐藏选择动画
            cycleView.hidden = true
            

        } else {
            //  有一个为nil,说明这个页面就是首页,开启动画
            startAnimation()
        }
        
    }
    // MARK:    点击事件
    @objc private func registerAction() {
        delegate?.registerButtonSelected()
    }
    
    @objc private func loginAction() {
        delegate?.loginButtonSelected()
    }
    
    //  之前代码约束代码
    private func test() {
        iconView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
        cycleView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: cycleView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: cycleView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        
        
        shadowView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: shadowView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: shadowView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        
        
        
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: cycleView, attribute: .Bottom, multiplier: 1, constant: 0))
        
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Leading, relatedBy: .Equal, toItem: messageLabel, attribute: .Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Trailing, relatedBy: .Equal, toItem: messageLabel, attribute: .Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        

    }
    
}
